
package com.insigma.business.qrcode.soldier._provider.impl;


import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import com.insigma.business.qrcode.common.dto.GovOrgBSearcher;
import com.insigma.business.qrcode.common._provider.BaseProviderImpl;
import com.insigma.business.qrcode.common._provider.CommonAdminAreaProvider;
import com.insigma.business.qrcode.common.service.CommonAdminAreaService;
import com.insigma.business.qrcode.common.service.GovOrgService;
import com.insigma.business.qrcode.common.util.AdminAreaCodeUtils;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.PinyinUtils;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier.dto.GovOrgDTO;
import com.insigma.business.qrcode.soldier.dto.SoldierBasicInfoDTO;
import com.insigma.business.qrcode.soldier.entity.GovOrgDO;
import com.insigma.business.qrcode.soldier._provider.GovEmployeeProvider;
import com.insigma.business.qrcode.soldier.service.GovEmployeeService;
import com.insigma.business.qrcode.soldier._provider.GovOrgProvider;
import com.insigma.business.qrcode.common.converter.GovOrgProviderConverter;
import com.insigma.business.qrcode.soldier.util.*;
import com.insigma.business.qrcode.welfare.util.LocationUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Service
@Component("govOrgProvider")
public class GovOrgProviderImpl extends BaseProviderImpl implements GovOrgProvider {

    @Autowired
    private GovOrgService govOrgService;

    @Autowired
    private GovEmployeeProvider govEmployeeProvider;

    @Autowired
    private CommonAdminAreaProvider commonAdminAreaProvider;

    @Autowired
    private CommonAdminAreaService commonAdminAreaService;

    @Autowired
    private GovEmployeeService govEmployeeService;

    @Resource
    RedisLock redisLock;

    private GovOrgDO recursionGetSameLevelGov(GovOrgDO govOfEmploy, String targetAdminAreaCode) {
        if (govOfEmploy == null) {
            return null;
        }
        if (Strings.isBlank(targetAdminAreaCode)) {
            return govOfEmploy;
        }

        GovOrgDO lastT = govOfEmploy;

        while (true) {
            if (Strings.isNotBlank(lastT.getParentOrgId())) {
                GovOrgDO parent = govOrgService.get(lastT.getParentOrgId());
               if (parent != null) {
                   String parentLeafAdminAreaId = parent.getLeafAdminAreaId();
                   if (parentLeafAdminAreaId.equals(targetAdminAreaCode)) {
                       lastT = parent;
                   } else {
                       return lastT;
                   }
               } else {
                   return lastT;
               }
            } else {
                return lastT;
            }
        }

    }


    @Override
    public BaseResponse<List<GovOrgDTO>> listByEmployeeId(String employeeId) {

        BizCheckUtil.checkNotNull(employeeId, "逻employeeId不能为空");
        List<GovOrgDO> list = govOrgService.listByEmployeeId(employeeId);
        final List<GovOrgDTO> govOrgDTOList = GovOrgProviderConverter.convertResponseDOList2DTOList(list);
        return ResponseUtils.buildSuccessResponse(govOrgDTOList);
    }

    @Override
    public BaseResponse importFwz() {
        List<String> list = FileUtil.readLines("C:\\Users\\jin\\Desktop\\tmp\\桐庐.txt", "UTF-8");
        String parentId = null;
        String parentAreaName = null;
        String rootArea = null;
        String rootId = null;
        for (int i = 0; i < list.size(); i++) {
            String item = list.get(i);
            if (Strings.isBlank(item)) {
                continue;
            }
            item = item.trim();
            List<String> split = getSimpleItem(item.split("  "));
            if (CollectionUtils.isEmpty(split)) {
                continue;
            }

            String name = null;
            String address = null;
            String contactName = null;
            String mobile = null;
            if (split.size() == 3) {
                name = split.get(0);
                address = split.get(1);
                contactName = split.get(2);
                mobile = split.get(2);
            } else if (split.size() == 4) {
                name = split.get(1);
                address = split.get(2);
                contactName = split.get(3);
                mobile = split.get(3);
            } else if (split.size() == 5) {
                name = split.get(1);
                address = split.get(2);
                contactName = split.get(3);
                mobile = split.get(4);
            } else {
                System.out.println(item);
                continue;
            }

            String adq = "";
            if (i == 0) {
                adq = name;
            } else if (name.contains("、")) {
                adq = rootArea + name;
            } else {
                adq = rootArea + parentAreaName + name;
            }

            SoldierBasicInfoDTO soldierBasicInfoDTO = new SoldierBasicInfoDTO();
            soldierBasicInfoDTO.setResidentAddress(adq);
            String areaCode = commonAdminAreaProvider.analyseAreaCodeByBasicInfo(soldierBasicInfoDTO).getDto();
            if (i == 0) {
                GovOrgDO father = govOrgService.getByAreaCodeAndId(areaCode, "2020072800002101");
                parentId = father.getId();
                father.setAddress(address);
                father.setContactNum(mobile);
                father.setContactPerson(contactName);
                govOrgService.update(father);
                parentAreaName = father.getName();
                rootArea = father.getName();
                rootId = father.getId();
                continue;
            }

            if (name.contains("、")) {
                name = name.substring(name.indexOf("、") + 1);
                GovOrgDO govOrgDO = govOrgService.get(areaCode);
                if (AdminAreaCodeUtils.getLevel(areaCode) != 4) {
                    areaCode = null;
                }

                GovOrgDO fwzDo = new GovOrgDO();
                fwzDo.setName(name);
                fwzDo.setAddress(address);
                fwzDo.setContactNum(mobile);
                fwzDo.setContactPerson(contactName);
                fwzDo.setVersion(1);
                fwzDo.setType("FWZ");
                fwzDo.setParentOrgId(rootId);
                fwzDo.setLeafAdminAreaId(areaCode);
                fwzDo.setLeafAdminAreaValue(govOrgDO != null ? govOrgDO.getLeafAdminAreaValue() : null);
                fwzDo.setStatus("ENABLED");
                fwzDo.setSelfMaintain(false);
                govOrgService.insert(fwzDo);
                parentId = fwzDo.getId();
                parentAreaName = fwzDo.getName();
                continue;
            } else {
                GovOrgDO govOrgDO = govOrgService.get(areaCode);
                if (AdminAreaCodeUtils.getLevel(areaCode) != 5) {
                    areaCode = null;
                }

                GovOrgDO fwzDo = new GovOrgDO();
                fwzDo.setName(name);
                fwzDo.setAddress(address);
                fwzDo.setContactNum(mobile);
                fwzDo.setContactPerson(contactName);
                fwzDo.setVersion(1);
                fwzDo.setType("FWZ");
                fwzDo.setParentOrgId(parentId);
                fwzDo.setLeafAdminAreaId(areaCode);
                fwzDo.setLeafAdminAreaValue(govOrgDO != null ? govOrgDO.getLeafAdminAreaValue() : null);
                fwzDo.setStatus("ENABLED");
                fwzDo.setSelfMaintain(false);
                govOrgService.insert(fwzDo);
                continue;
            }
        }

        return null;
    }


















    @Override
    public BaseResponse addNewOrgOfFwz() {
        List<String> list = FileUtil.readLines("C:\\Users\\jin\\Desktop\\xxx", "UTF-8");
        Long id = 2022010700004421L;
        for (String item : list) {
            String[] split = item.split(",");
            String name = split[0].trim();
            String address = split[1].trim();
            String lng = split[2].trim();
            String lat = split[3].trim();
            String contactNo = split[4].trim();
            String contactPerson = split[5].trim();
            String parentId = split[6].trim();

            GovOrgDO govOrgDO = new GovOrgDO();
            govOrgDO.setParentOrgId(parentId);
            govOrgDO.setType("TYJ");
            govOrgDO.setVersion(1);
            govOrgDO.setName(name);
            govOrgDO.setAddress(address);
            govOrgDO.setLongitude(Double.valueOf(lng));
            govOrgDO.setLatitude(Double.valueOf(lat));
            govOrgDO.setContactPerson(contactPerson);
            govOrgDO.setContactNum(contactNo);
            govOrgDO.setSelfMaintain(false);
            GovOrgDO father = govOrgService.get(govOrgDO.getParentOrgId());
            name = name.trim();
            name = name.replace("　", "");
            name = name.replace(" ", "");
            String s = father.getCode() + "/" + PinyinUtils.getAlpha(name);
            govOrgDO.setCode(s);
            govOrgDO.setLevel(father.getLevel() + 1);
            govOrgDO.setStatus("ENABLED");

            govOrgService.insert(govOrgDO);
            System.out.println();
        }
        return null;
    }


    @Override
    public BaseResponse addLocationOfBaiduMap() {
        String fwzCode = "/STYJRSWJ/STYJRFWZX/QTXQFWZX";
        String parentAddress = "杭州市钱塘区";
        String urlTemplate = "https://api.map.baidu.com/place/v2/search?" +
                "query=%s" +
                "&region=杭州" +
                "&output=json&ak=pXlEit6Ca8HHeVgeS647nmpjFFfAmriF" +
                "&location=30.276441,120.495305&radius=300000" +
                "&page_num=%d";

        List<GovOrgDO> list = govOrgService.listSubStationByCode(fwzCode);
        for (GovOrgDO govOrgDO : list) {
            if (govOrgDO.getLatitude() == null || govOrgDO.getLongitude() == null) {
                int pageNo = 0;
                String queryName = govOrgDO.getName().replace("杭州市", "") + "退役";
                while (true) {
                    String url = String.format(urlTemplate, queryName, pageNo++);
                    String result = OkHttp3Util.sendByGetUrl(url);
                    JSONObject object = JSONObject.parseObject(result);
                    Integer total = object.getInteger("total");
                    JSONArray array = object.getJSONArray("results");
                    if (array != null && array.size() > 0) {
                        for (int i = 0; i < array.size(); i++) {
                            JSONObject jsonObject = array.getJSONObject(i);
                            String name = jsonObject.getString("name");
                            String address = jsonObject.getString("address");
                            String city = jsonObject.getString("city");
                            String area = jsonObject.getString("area");
                            String telephone = jsonObject.getString("telephone");
                            String uid = jsonObject.getString("uid");
                            JSONObject location = jsonObject.getJSONObject("location");
                            Double lat = location.getDouble("lat");
                            Double lng = location.getDouble("lng");
                            System.out.println();
                            if (name.contains("退役")) {
                                String areaCode = commonAdminAreaProvider.analyseAreaCodeByBasicInfo(parentAddress + name);
                                updateLocation(areaCode, list, lng, lat);
                            }
                        }
                    } else {
                        break;
                    }
                }
            }
        }
        for (GovOrgDO govOrgDO : list) {
            govOrgService.update(govOrgDO);
        }

        return null;
    }

    @Override
    public BaseResponse addLocationOfGaodeMap() {
        String fwzCode = "/STYJRSWJ/STYJRFWZX/YHQFWZX";
        String parentAddress = "杭州市余杭区";
        String urlTemplate = "https://restapi.amap.com/v3/place/text?keywords=%E9%80%80%E5%BD%B9&city=%E4%BD%99%E6%9D%AD&offset=100&page=1&key=13142827ff3ae59c4ece122f49073b2b";
        Map<String, String> header = Maps.newLinkedHashMap();
        header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36");
        String result = OkHttp3Util.sendByGetUrlWithHeader(urlTemplate, header);
        JSONObject object = JSONObject.parseObject(result);
        Integer total = object.getInteger("count");
        JSONArray array = object.getJSONArray("pois");
        if (array != null && array.size() > 0) {
            for (int i = 0; i < array.size(); i++) {
                JSONObject jsonObject = array.getJSONObject(i);
                String name = jsonObject.getString("name");
                String address = jsonObject.getString("address");
                String cityname = jsonObject.getString("city");
                String adname = jsonObject.getString("adname");
                String uid = jsonObject.getString("id");
                String location = jsonObject.getString("location");
                String[] split = location.split(",");
                Double gdlng = Double.valueOf(split[0]);
                Double gdlat = Double.valueOf(split[1]);

                double[] doubles = LocationUtils.gaodeToBaidu(gdlng, gdlat);
                double bd_lng = doubles[0];
                double bd_lat = doubles[1];

                JSONArray photos = jsonObject.getJSONArray("photos");
                if (photos != null && photos.size() > 0) {
                    for (int j = 0; j < photos.size(); j++) {
                        JSONObject photosJSONObject = photos.getJSONObject(j);
                        photosJSONObject.getString("url");
                        photosJSONObject.put("name", "图片" + j);
                        photosJSONObject.remove("title");
                    }
                }
                String img = photos.toJSONString();
                System.out.println(img);
                if (name.contains("退役")) {
                    String areaCode = commonAdminAreaProvider.analyseAreaCodeByBasicInfo(parentAddress + name);
                    if (areaCode.startsWith("330110") && AdminAreaCodeUtils.getLevel(areaCode) > 3) {
                        GovOrgDO byAreaCodeAndId = govOrgService.getByAreaCodeAndId(areaCode, "2020072800002101");
                        byAreaCodeAndId.setLatitude(bd_lat);
                        byAreaCodeAndId.setLongitude(bd_lng);
                        byAreaCodeAndId.setAddress(address);
                        byAreaCodeAndId.setImg(img);
                        govOrgService.update(byAreaCodeAndId);
                    }
                }
            }
        }

        return null;
    }

    private void updateLocation(String areaCode, List<GovOrgDO> list, Double lng, Double lat) {
        for (GovOrgDO govOrgDO : list) {
            if (govOrgDO.getLeafAdminAreaId().equals(areaCode) && govOrgDO.getLatitude() == null && govOrgDO.getLongitude() == null) {
                govOrgDO.setLongitude(lng);
                govOrgDO.setLatitude(lat);
            }
        }
    }

    @Override
    public BaseResponse addDetail() {
        ExcelReader reader = ExcelUtil.getReader("E:\\download\\【01】服务站信息\\【01】服务站信息\\崇贤街道\\220112上报服务站机构信息.xls");
        final List<List<Object>> read = reader.read();

        List<Object> notInDb = Lists.newArrayList();
        List<Object> morethen2 = Lists.newArrayList();
        System.out.println("read.size() " + read.size());
        for (int i = 1; i < read.size(); i++) {
            List<Object> objects = read.get(i);

            List<String> items = (List) objects;
            String name = items.get(0);
            String address = items.get(1);
            String location = items.get(2);
            String contactNo = String.valueOf(items.get(3));
            String contactPerson = items.get(4);

            String[] split = location.split(",");
            Double lng = Double.valueOf(split[0]);
            Double lat = Double.valueOf(split[1]);

            System.out.println(split.length);

            String shortName = name.replace("余杭区", "").replace("退役军人服务站", "").replace("社区", "");
            String longName = "杭州市" + name;

            if (shortName.length() <= 4) {
                String adminAreaCode = commonAdminAreaProvider.analyseAreaCodeByBasicInfo(longName);
                if (AdminAreaCodeUtils.getLevel(adminAreaCode) != 4) {
                    System.out.println("error:" + objects);
                }

                List<GovOrgDO> list = govOrgService.listStationByAdminAreaCode(adminAreaCode);
                if (CollectionUtils.isEmpty(list)) {
                    notInDb.add(objects);
                } else if (list.size() > 1) {
                    morethen2.add(objects);
                } else {
                    GovOrgDO govOrgDO = list.get(0);
                    govOrgDO.setAddress(address);
                    govOrgDO.setContactNum(contactNo.trim());
                    govOrgDO.setContactPerson(contactPerson.trim());
                    govOrgDO.setLongitude(lng);
                    govOrgDO.setLatitude(lat);
                    govOrgService.update(govOrgDO);
                }
            } else {
                List<GovOrgDO> list = govOrgService.listByNameLikeOfYh(shortName, "/STYJRSWJ/STYJRFWZX/LPQFWZX");
                if (CollectionUtils.isEmpty(list)) {
                    notInDb.add(objects);
                } else if (list.size() > 1) {
                    morethen2.add(objects);
                } else {
                    GovOrgDO govOrgDO = list.get(0);
                    govOrgDO.setAddress(address);
                    govOrgDO.setContactNum(contactNo.trim());
                    govOrgDO.setContactPerson(contactPerson.trim());
                    govOrgDO.setLongitude(lng);
                    govOrgDO.setLatitude(lat);
                    govOrgService.update(govOrgDO);
                }
            }
        }

        System.out.println("-----------");
        for (Object o : notInDb) {
            System.out.println(o);
        }
        return null;
    }

    private File getFile(File[] files, String name) {
        for (File file : files) {
            if (file.getName().contains(name)) {
                return file;
            }
        }
        return null;
    }


    @Override
    public BaseResponse updateDetailOrInsert() {
        standardDateImport();

        return null;
    }

    @Override
    public BaseResponse freshAdminAreaCode() {
        log.info("刷新地区码");
        ThreadPoolManager.getInstance().submit(new Runnable() {
            @Override
            public void run() {
                try {
                    GovOrgBSearcher searcher = new GovOrgBSearcher(1, 100000);
                    List<GovOrgDO> search = govOrgService.search(searcher);
                    Map<String, GovOrgDO> govMap = search.stream().collect(Collectors.toMap(GovOrgDO::getId, s -> s));

                    for (GovOrgDO govOrgDO : search) {
                        if (Strings.isBlank(govOrgDO.getLeafAdminAreaId())) {
                            GovOrgDO father = govMap.get(govOrgDO.getParentOrgId());
                            if (father == null) {
                                continue;
                            }

                            String fullAddress = recurseFullPath(govMap, govOrgDO);
                            String analyAminCode = commonAdminAreaProvider.analyseAreaCodeByBasicInfo(fullAddress);

                            if (AdminAreaCodeUtils.checkIsFatherArea(father.getLeafAdminAreaId(), analyAminCode)) {
                                govOrgDO.setLeafAdminAreaId(analyAminCode);
                            } else {
                                govOrgDO.setLeafAdminAreaId(father.getLeafAdminAreaId());
                            }
                            govOrgService.update(govOrgDO);
                        }
                    }
                }catch (Exception e) {
                    log.error("{}", e);
                }
            }
        });

        return ResponseUtils.buildSuccessResponse(null);
    }

    private String recurseFullPath( Map<String, GovOrgDO> govMap, GovOrgDO govOrgDO) {
        if (govOrgDO == null) {
            return "";
        }

        String parentOrgId = govOrgDO.getParentOrgId();
        if (Strings.isNotBlank(parentOrgId)) {
            GovOrgDO parentGov = govMap.get(parentOrgId);
            return recurseFullPath(govMap, parentGov) + govOrgDO.getName();
        }

        return govOrgDO.getName();
    }

    public static void main(String[] args) {
        new GovOrgProviderImpl().standardDateImport();
    }

    public void recursionUploadImg() {
        List<String> strings = FileUtil.readLines("C:\\Users\\jin\\Desktop\\hh", "UTF-8");
        int count = 1;
        for (String string : strings) {
            System.out.println(count++ + "--------------------");
            String[] split = string.split(",");
            String id = split[0];
            String name = split[1];
            String path = split[2];
            Collection<File> files = FileUtils.listFiles(new File(path), new String[]{"jpg", "png", "JPG", "PNG"}, false);
            JSONArray array = new JSONArray();
            for (File file : files) {
                String relativeUrl = TwoLevelCheckUtils.getRelativeUrl(file.getName(), file.getPath());
                JSONObject o = new JSONObject();
                o.put("name", file.getName());
                o.put("url", relativeUrl);
                array.add(o);
            }
            GovOrgDO govOrgDO = govOrgService.get(id);
            govOrgDO.setImg(JSON.toJSONString(array));
            govOrgService.update(govOrgDO);
        }
    }

    private BaseResponse standardDateImport() {
        String basePath = "C:\\Users\\jin\\Desktop\\运河红星家园提供资料\\导航";
        String parentId = "2020072800002122";

        File file = new File(basePath);
        for (File jdFile : file.listFiles()) {
            String jdName = jdFile.getName();
            System.out.println(jdName);
            File[] allDirFiles = jdFile.listFiles();
            File xls = getXls(allDirFiles);
            ExcelReader reader = ExcelUtil.getReader(xls);

            String baseCode = "";
            String fatherName = "";
            String fatherCode = "";

            List<String> picPaths = Lists.newArrayList();
            List<String> noPicPaths = Lists.newArrayList();
            List<List<String>> notInDb = Lists.newArrayList();
            List<Object> morethen2 = Lists.newArrayList();

            final List<List<Object>> read = reader.read();
            System.out.println("read.size() " + read.size());
            for (int i = 1; i < read.size(); i++) {

                List<Object> objects = read.get(i);
                int size = objects.size();
                for (int j = size; j < 7; j++) {
                    objects.add(null);
                }

                System.out.println(objects);
                List<String> items = (List) objects;
                String name = items.get(0);
                if (name == null) {
                    continue;
                }
                String address = items.get(1);
                String location = items.get(2);
                String contactNo = String.valueOf(items.get(3));
                String contactPerson = items.get(4) == null ? "" : items.get(4);
                String soldierNum = items.get(5) == null ? null : String.valueOf(items.get(5)).replace("人", "").replace("无", "");
                String info = items.get(6);

                String[] split = null;
                if (location.contains("|")) {
                    split = location.split("\\|");
                } else {
                    split = location.replace("，", ",").split(",");
                }
                if (location.contains("查询地址")) {
                    System.out.println();
                }
                Double lng = Double.valueOf(split[0]);
                Double lat = Double.valueOf(split[1]);

                BizCheckUtil.checkIsTrue(split.length == 2, "长度错误");
                System.out.println(split.length);

                String shortName = name.replace("杭州市", "").replace("拱墅区", "")
                        .replace("退役军人服务站", "")
                        .replace("社区", "").replace("退役军人服务中心", "")
                        .replace("退伍军人服务站", "");
                String longName = "杭州市拱墅区" + jdName + name;

                if (i == 1) {

                    String adminAreaCode = commonAdminAreaProvider.analyseAreaCodeByBasicInfo(longName);
                    if (AdminAreaCodeUtils.getLevel(adminAreaCode) != 4) {
                        System.out.println("error:" + objects);
                    }

                    List<GovOrgDO> list = govOrgService.listStationByAdminAreaCode(adminAreaCode);
                    if (CollectionUtils.isEmpty(list)) {
                        notInDb.add(items);
                    } else if (list.size() > 1) {
                        morethen2.add(objects);
                    } else {

                        GovOrgDO govOrgDO = list.get(0);
                        govOrgDO.setName(name);
                        govOrgDO.setAddress(address);
                        govOrgDO.setContactNum(contactNo.trim());
                        govOrgDO.setContactPerson(contactPerson.trim());
                        govOrgDO.setLongitude(lng);
                        govOrgDO.setLatitude(lat);
                        govOrgDO.setSoldierNum(FieldUtils.objectToInteger(soldierNum));
                        govOrgDO.setIntroduction(info);
                        String allImg = getAllImg(jdFile.getPath(), shortName + "退役");
                        govOrgDO.setImg(allImg);
                        govOrgService.update(govOrgDO);

                        fatherCode = govOrgDO.getCode();
                        fatherName = govOrgDO.getName().replace("退役军人服务站", "").replace("退役", "").replace("军人服务站", "");
                    }
                } else {
                    if (Strings.isNotBlank(fatherName)) {

                        shortName = shortName.replace(fatherName, "").replace(" ", "");
                    }
                    List<GovOrgDO> list = govOrgService.listByNameLikeOfYh(shortName, fatherCode);
                    if (CollectionUtils.isEmpty(list)) {
                        GovOrgDO govOrgDO = new GovOrgDO();
                        govOrgDO.setParentOrgId(parentId);
                        govOrgDO.setType("TYJ");
                        govOrgDO.setVersion(1);
                        govOrgDO.setName(name);
                        govOrgDO.setAddress(address);
                        govOrgDO.setLongitude(Double.valueOf(lng));
                        govOrgDO.setLatitude(Double.valueOf(lat));
                        govOrgDO.setContactPerson(contactPerson);
                        govOrgDO.setContactNum(contactNo);
                        govOrgDO.setSelfMaintain(false);
                        govOrgDO.setSoldierNum(FieldUtils.objectToInteger(soldierNum));
                        govOrgDO.setIntroduction(info);

                        String adminAreaCode = commonAdminAreaProvider.analyseAreaCodeByBasicInfo(longName);
                        if (AdminAreaCodeUtils.getLevel(adminAreaCode) != 5) {
                            System.out.println("error:" + objects);
                        }
                        govOrgDO.setLeafAdminAreaId(adminAreaCode);
                        GovOrgDO father = govOrgService.get(govOrgDO.getParentOrgId());
                        name = name.trim();
                        name = name.replace("　", "");
                        name = name.replace(" ", "");
                        String s = father.getCode() + "/" + PinyinUtils.getAlpha(name);
                        govOrgDO.setCode(s);
                        govOrgDO.setLevel(father.getLevel() + 1);
                        govOrgDO.setStatus("ENABLED");
                        String allImg = getAllImg(jdFile.getPath(), shortName);
                        govOrgDO.setImg(allImg);
                        govOrgService.insert(govOrgDO);
                    } else if (list.size() > 1) {
                        morethen2.add(objects);
                    } else {

                        GovOrgDO govOrgDO = list.get(0);
                        govOrgDO.setName(name);
                        govOrgDO.setAddress(address);
                        govOrgDO.setContactNum(contactNo.trim());
                        govOrgDO.setContactPerson(contactPerson.trim());
                        govOrgDO.setLongitude(lng);
                        govOrgDO.setLatitude(lat);
                        govOrgDO.setSoldierNum(FieldUtils.objectToInteger(soldierNum));
                        govOrgDO.setIntroduction(info);
                        String allImg = getAllImg(jdFile.getPath(), shortName);
                        govOrgDO.setImg(allImg);
                        govOrgService.update(govOrgDO);
                    }
                }
            }

            System.out.println("-----------");
            for (Object o : notInDb) {
                System.out.println(o);
            }
            System.out.println("-----------");

            System.out.println("----noPicPaths-------");
            for (Object o : noPicPaths) {
                System.out.println(o);
            }
            System.out.println("----noPicPaths end-------");
        }


        return null;
    }

    private String getAllImg(String path, String name) {
        Collection<File> files = FileUtils.listFiles(new File(path), new String[]{"jpg", "png", "JPG", "PNG"}, false);
        JSONArray array = new JSONArray();
        for (File file : files) {
            if (file.getName().contains(name)) {
                String relativeUrl = TwoLevelCheckUtils.getRelativeUrl(file.getName(), file.getPath());
                JSONObject o = new JSONObject();
                o.put("name", file.getName());
                o.put("url", relativeUrl);
                array.add(o);
            }
        }
        return JSON.toJSONString(array);
    }

    private File getXls(File[] files) {
        for (File file : files) {
            if (file.getName().toLowerCase().contains("xls")) {
                return file;
            }
        }
        return null;
    }
























































































































































    private List<List<Object>> getAllXlsInfo(String basePath) {

        String[] ext = new String[]{"xls", "XLS"};
        Collection<File> files = FileUtils.listFiles(new File(basePath), ext, true);
        for (File file : files) {

        }
        return null;
    }

    private void getImgPath(File[] files, List<String> picPaths, List<String> noPicPaths, String name, GovOrgDO govOrgDO, String shortName) {
        File file = getFile(files, shortName);
        if (file == null) {
            noPicPaths.add(govOrgDO.getId() + "," + name + ",");
        } else {
            picPaths.add(govOrgDO.getId() + "," + name + "," + file.getPath());
        }
    }


    private GovOrgDO updateGovInfo(String address, String contactNo, String contactPerson, Object soldierNum, String info, Double lng, Double lat, List<GovOrgDO> list) {
        GovOrgDO govOrgDO = list.get(0);
        govOrgDO.setAddress(address);
        govOrgDO.setContactNum(contactNo.trim());
        govOrgDO.setContactPerson(contactPerson.trim());
        govOrgDO.setLongitude(lng);
        govOrgDO.setLatitude(lat);
        govOrgDO.setSoldierNum(FieldUtils.objectToInteger(soldierNum));
        govOrgDO.setIntroduction(info);
        govOrgService.update(govOrgDO);
        return govOrgDO;
    }

    @Override
    public BaseResponse cleanPinyin() {
        boolean lock = redisLock.tryLock("pinyin", 60000);
        if (!lock) {
            return null;
        }

        List<GovOrgDO> search = govOrgService.search(new GovOrgBSearcher(1, 10000));
        Map<String, GovOrgDO> collect = search.stream().collect(Collectors.toMap(GovOrgDO::getId, s -> s));
        for (GovOrgDO govOrgDO : search) {
            String code = getLevelCode(govOrgDO, collect);
            govOrgDO.setCode(code);
        }

        for (GovOrgDO govOrgDO : search) {
            int i = matchCount(govOrgDO.getCode(), "/");
            govOrgDO.setLevel(i);
        }
        for (GovOrgDO govOrgDO : search) {
            govOrgService.update(govOrgDO);
        }

        return null;
    }

    public int matchCount(String st, String character) {
        int count = (st.length() - st.replace(character, "").length()) / character.length();
        return count;
    }


    private String getLevelCode(GovOrgDO govOrgDO, Map<String, GovOrgDO> collect) {
        if (govOrgDO == null) {
            return "";
        }
        if (Strings.isNotBlank(govOrgDO.getCode())) {
            return govOrgDO.getCode();
        }

        String parentOrgId = govOrgDO.getParentOrgId();
        if (Strings.isBlank(parentOrgId)) {
            if (Strings.isNotBlank(govOrgDO.getCode())) {
                return govOrgDO.getCode();
            }
            return "/" + PinyinUtils.getAlpha(govOrgDO.getName());
        }

        GovOrgDO parent = collect.get(parentOrgId);
        if (Strings.isBlank(govOrgDO.getName())) {
            return "";
        }
        try {
            String name = govOrgDO.getName();
            name = name.trim();
            name = name.replace("　", "");
            name = name.replace(" ", "");
            String s = getLevelCode(parent, collect) + "/" + PinyinUtils.getAlpha(name);
            return s;
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }

    private List<String> getSimpleItem(String[] s) {
        List<String> list = new ArrayList();
        for (String s1 : s) {
            if (StringUtils.isNotBlank(s1)) {
                String[] split = s1.trim().split("\t");
                for (String s2 : split) {
                    if (StringUtils.isNotBlank(s2.trim())) {
                        list.add(s2.trim());
                    }
                }
            }
        }
        return list;
    }
}
